import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Vector;
import java.io.*;

import javax.swing.text.Document;

	public class MyLexicalAnalyzer implements LexicalAnalyzer {
    	ArrayList<String> listTags = new ArrayList<String>();
    	boolean stop = false;
    	
    	//MySyntaxAnalyzer syntax = new MySyntaxAnalyzer();
    	//public MySyntaxAnalyzer syntax;
    	
		String currentChar = ""; 
        
        //all the tags that are valid are placed here. when the lookup token is called, it comes back here to return true or false.
		MyLexicalAnalyzer() {
        	listTags.add("#START DOCUMENT");
        	listTags.add("#END DOCUMENT");
        	listTags.add("#START HEAD");
        	listTags.add("END HEAD");
        	listTags.add("#START TITLE");
        	listTags.add("#END TITLE");
        	listTags.add("#START PARAGRAPH");
        	listTags.add("#END PARAGRAPH");
        	listTags.add("#START BOLD");
        	listTags.add("#END BOLD");
        	listTags.add("#START ITALICS");
        	listTags.add("#END ITALICS");
        	listTags.add("#START LIST");
        	listTags.add("#END LIST");
        	listTags.add("#START ITEM");
        	listTags.add("#END ITEM");
        	listTags.add("#NEWLINE");
        	listTags.add("#START AUDIO");
        	listTags.add("#END AUDIO");
        	listTags.add("#START VIDEO");
        	listTags.add("#END VIDEO");
        	listTags.add("#START DEFINE");
        	listTags.add("#NAME");
        	listTags.add("VALUE");
        	listTags.add("#END DEFINE");
        	listTags.add("#START USE");
        	listTags.add("#END USE");
        	
        }
        
		//this is the method that is called when in the syntax analyzer begins to process. Every time the syntax analyzer gets a token
		//and processes it, it calls this method to retreive the next token. I had to insert a for loop so that the first
		//character, otherwise the first hashtag would be missed and would completely throw off the whole process. The doText and doTags
		//are explained below.However, I believe within these methods is why my program goes in an infinite loop and produces no output.
		//I could not figure out how to fix this unfortunately.
        @Override
        public void getNextToken() throws IOException, CompilerException {
        	for (int i = 1; i > 0; i--){
        		getCharacter();
        	}
        	doText();
        	doTag();
        }
        	
        //this method gets the next character from the string that is produced from the files that are being read in.
        //I created a currentposition int that keeps track of which character is being parsed at the moment. This method is called
        //after everytime character is evaluated/added. Becuase I couldn't figure out why my program was going in an infinite loop,
        //I added a System.exit(0) to stop looping.
        @Override
        public void getCharacter() throws IOException{
        	if (Compiler.CurrentPosition < Compiler.fulltext.length()){
        		char thisChar = Compiler.fulltext.charAt(Compiler.CurrentPosition);
        		currentChar = Character.toString(thisChar);
        		Compiler.CurrentPosition++;
        	}
        	else {
        		System.exit(0);
        	}
        }
        
        //This method begins only when it recognizes a hashtag. It clears out the current token and adds the character at hand.
        //It retreives the next character and checks to makes ure it IS NOT a hastag which will mean that it could be a tag. The
        //this is added then we check for a space, which will lead to checking for a start or end tag. If it is not these, then it will 
        //go to newline. If it is start or end, it will continue to gather character untilthe next space. Next method is then applied.
		public void doTag() throws IOException, CompilerException{
        	if (currentChar.equalsIgnoreCase("#")){
        		Compiler.token = "";
        		Compiler.token += currentChar;
        		getCharacter();
        		while ((!(currentChar.equalsIgnoreCase("#")) && (!(currentChar.equalsIgnoreCase(" "))))){
        			Compiler.token += currentChar;
        			getCharacter();
        			boolean spaceExists = isSpace(currentChar);
        			if (spaceExists == false) {
        				if (!(Compiler.token.equalsIgnoreCase("#NEWLINE"))){
        				Compiler.token+=currentChar;
        				getCharacter();
        				}
        				else {
        					newline();
        				}
        			}
        			else {
        				if (Compiler.token.equalsIgnoreCase("#START") || Compiler.token.equalsIgnoreCase("#END")) {
        					Compiler.token += currentChar;
        					getCharacter();
        					while (!(currentChar.equalsIgnoreCase(" "))){
        						Compiler.token += currentChar;
        						getCharacter();
        				
        					}
        		        	if (lookupToken() == true) {
        						//System.out.println("Success");
        					}
        					else {
        						System.err.println("Error: this token is invalid");
        						System.exit(0);
        					}
        				}
        				else {
    						System.err.println("Error: this token is invalid");
    						System.exit(0);        				
    					}
        			}
        		}
        	}
		}

		//This method is used when there is no hashtag seen. It is taken as text and gathers them until it does see a hashtag.
		//When the hashtag is recognized, the program sould go back to the syntax analyzer and continue its checking.
        @Override
        public void doText() throws IOException, CompilerException {
        	while (!(currentChar.equalsIgnoreCase("#"))){
        		Compiler.token += currentChar;
        		getCharacter();
        	}
        }
        		
       //this method is made becuase nothing should be expected after the newline tag. There are no spaces nor text.  				
        public void newline() throws IOException, CompilerException {
        	 if (listTags.contains(Compiler.token)) {
                 //syntax.document();
        	}
       	}

        //this method is used simply to check for a space. It is called within the doTag method and returns true or false.
        @Override
        public boolean isSpace(String c) {
        	if (currentChar.equalsIgnoreCase(" ")){
        		return true;
        	}
        	else {
                return false;
        	}
        }
        
        //this method calls the constructor holding all of the tags to compare the token at hand with. If it does not exist, it returns 
        //false and the program ends becuase the file has an illegal token.
        @Override
        public boolean lookupToken() {
        	String c = Compiler.token.toUpperCase();
            if (listTags.contains(c))
            	return true;
            else 
            	return false;
        }

	}